package com.yuexunit.mobilenurse.module.SkinTest.presenter.impl;

import com.alibaba.fastjson.JSON;
import com.yuexunit.mobilenurse.module.Inspect.bean.InjectionList_Exdetail;
import com.yuexunit.mobilenurse.module.Inspect.bean.InjectionList_Exreqlist;
import com.yuexunit.mobilenurse.module.Inspect.bean.YXInjectionListBean;
import com.yuexunit.mobilenurse.module.Inspect.model.IInspectModel;
import com.yuexunit.mobilenurse.module.Inspect.ui.view.IInspectView;
import com.yuexunit.mobilenurse.module.SkinTest.bean.SkinTestListBean;
import com.yuexunit.mobilenurse.module.SkinTest.bean.YXSkinTestListBean;
import com.yuexunit.mobilenurse.module.SkinTest.model.ISkinTestListModel;
import com.yuexunit.mobilenurse.module.SkinTest.presenter.ISkinTestListPresenter;
import com.yuexunit.mobilenurse.module.SkinTest.ui.view.ISkinTestListView;

import org.kymjs.kjframe.ui.ViewInject;

import java.util.ArrayList;
import java.util.List;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * Created by Administrator on 2018/3/14 0014.
 */

public class SkinTestListPresenter implements ISkinTestListPresenter{
    private ISkinTestListView view;
    private ISkinTestListModel model;
    ArrayList<SkinTestListBean> skinTestListBeans  = new ArrayList<>();

    public SkinTestListPresenter(ISkinTestListView view,ISkinTestListModel model)
    {
        this.view = view;
        this.model = model;
    }

    @Override
    public void showSkinTestList(String visitno) {
        model.getSkinTestListData(visitno)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<String>() {
                               @Override
                               public void onCompleted() {

                               }

                               @Override
                               public void onError(Throwable e) {
                                   ViewInject.toast("暂无数据");
                                   view.showSkinTestList(skinTestListBeans);
                               }

                               @Override
                               public void onNext(String result) {

                                   YXSkinTestListBean Total = JSON.parseObject(result, YXSkinTestListBean.class);
                                   YXSkinTestListBean InspectData = Total.getBody();

                                   if ("0".equals(InspectData.getHead().getRet_code())) {

                                       if (skinTestListBeans != null) {
                                           skinTestListBeans.clear();
                                       }

                                       List<SkinTestListBean> injectionList_exdetails;

                                       String exreqlists = InspectData.getResponse().getInfo();
                                       String exreqlists_first = exreqlists.substring(0, 1);
                                       if (!exreqlists_first.equals("[")) {
                                           exreqlists = "[" + exreqlists + "]";
                                           injectionList_exdetails = JSON.parseArray(exreqlists, SkinTestListBean.class);
                                       } else {
                                           injectionList_exdetails = JSON.parseArray(exreqlists, SkinTestListBean.class);
                                       }


                                       for (int i = 0; i < injectionList_exdetails.size(); i++) {
                                           skinTestListBeans.add(injectionList_exdetails.get(i));
                                       }
                                       view.showSkinTestList(skinTestListBeans);
                                   } else {
                                       ViewInject.toast("暂无数据");
                                       view.showSkinTestList(skinTestListBeans);
                                   }

                               }
                           }
                );
    }
}
